<?php
    class AdminDthink_Model_Congratulations extends Zend_Db_Table_Abstract
    {
        protected $_name = 'congratulations';
        protected $_primary = 'id';
        protected $_db;
        
        public function __construct() {
            $this->_db = Zend_Registry::get('db');
        }
        
        public function listCongratulations() {
            $data = array(
                'id',
                'fullname',
                'content',
                'create_time',
                'status',
            );
            $sql = $this->_db->select()
                            ->from($this->_name, $data)
                            ->order('id DESC');
            return $sql;
        }
        
        public function getCongratulationsById($id) {
            $data = array(
                'id',
                'fullname',
                'content',
                'create_time',
                'status',
            );
            $sql = $this->_db->select()
                            ->from($this->_name, $data)
                            ->where('id = ?', $id);
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        
        public function createCongratulations($data) {
            $this->_db->insert($this->_name, $data);
        }
        
        public function editCongratulations($data, $id) {
            $where = $this->getAdapter()->quoteInto('id = ?', $id);
            $this->_db->update($this->_name, $data, $where);
        }
        
        public function deleteCongratulations($id) {
            $where = $this->getAdapter()->quoteInto('id = ?', $id);
            $this->_db->delete($this->_name, $where);
        }
        
        public function deleteManyCongratulations($listId) {
            foreach($listId as $value) {
                $where = $this->getAdapter()->quoteInto('id = ?', $value);
                $this->_db->delete($this->_name, $where);
            }
        }
        
        public function publishCongratulations($data, $id) {
            $where = $this->getAdapter()->quoteInto('id = ?', $id);
            $this->_db->update($this->_name, $data, $where);
        }
        
        public function searchCongratulations($keyword) {
            $keyword = (string) '%' . str_replace(' ', '%', $keyword) . '%';
            $data = array(
                'id',
                'fullname',
                'content',
                'create_time',
                'status',
            );
            $sql = $this->_db->select()
                            ->from($this->_name, $data)
                            ->where('fullname_plaintext LIKE ?', $keyword)
                            ->orWhere('content_plaintext LIKE ?', $keyword)
                            ->order('id ASC');
            return $sql;
        }
    }
